#include <signal.h>
#include <thread>
#include <chrono>
#include <cstdio>
#include "heartbeat_interface.h"

static bool g_exit = false;

static void sigroutine(int signo) { 
	switch (signo) {
		case SIGHUP:
			fprintf(stderr, "Get a signal -- SIGHUP ");
			g_exit = true;
		break;
		case SIGINT:
			fprintf(stderr, "Get a signal -- SIGINT \n");
			g_exit = true;
		break;
		case SIGQUIT:
			fprintf(stderr, "Get a signal -- SIGQUIT ");
			g_exit = true;
		break;
		case SIGSEGV:
			fprintf(stderr, "Get a signal -- SIGSEGV ");
			g_exit = true;
		break;
		case SIGTERM:
			fprintf(stderr, "Get a signal -- SIGTERM ");
			g_exit = true;
		break;	
	}
}

int main(int argc, char* argv[]) {
    signal(SIGHUP, sigroutine);
	signal(SIGINT, sigroutine);
	signal(SIGQUIT, sigroutine);
	//signal(SIGSEGV, sigroutine);
	signal(SIGTERM, sigroutine);
    
	heartbeat_interface_init(argv[0]);
	
	int interval = 10;
	heartbeat_interface_set_interval(interval);
	heartbeat_interface_start_detect();
	
    while(!g_exit) {
        std::this_thread::sleep_for(std::chrono::seconds(3));
		
		heartbeat_interface_send();
    }
	
	heartbeat_interface_deinit();
    return 0;
}